#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

class String{
public:
    String():_pstr(new char[1]()){

    }
    
    ~String(){
        if(_pstr){
            delete[] _pstr;
        }
    }

    String(const char * ps)
        :_pstr(new char[strlen(ps)+1]()){
            strcpy(_pstr,ps);
        }

    String(const String &rhs)
        :_pstr(new char[rhs.size()+1]()){
            strcpy(_pstr,rhs._pstr);
        }

    String(String &&rhs)
        :_pstr(rhs._pstr){
            rhs._pstr=nullptr;
        }

    String& operator=(const String &rhs)
        {
            if(this!=&rhs){
                delete[] _pstr;
                _pstr=new char[strlen(rhs._pstr)+1]();
                strcpy(_pstr,rhs._pstr);
            }
            return *this;
        }

    String& operator=(String &&rhs)
        {
            if(this!=&rhs){
                delete[] _pstr;
                _pstr=rhs._pstr;
                rhs._pstr=nullptr;
            }
            return *this;
        }

    String& operator=(const char *ps)
        {
            delete[] _pstr;
            _pstr=new char[strlen(ps)+1]();
            strcpy(_pstr,ps);
            return *this;
        }

    String& operator+=(const String &rhs){
        this->append(rhs);
        return *this;
    }


    friend ostream & operator<<(ostream &os,const String &rhs);
    friend istream & operator>>(istream &is,String &rhs);
    friend bool operator==(const String &lhs,const String &rhs);
    friend bool operator!=(const String &lhs,const String &rhs);
    friend bool operator>=(const String &lhs,const String &rhs);
    friend bool operator<=(const String &lhs,const String &rhs);
    friend String operator+(const String &lhs,const String &rhs);



    const size_t size()const{
        return strlen(_pstr);
    }

    bool empty() const{
        return strlen(_pstr)==0;
    }

    const char *c_str() const{
        return _pstr;
    }

    const char *data() const{
        return _pstr;
    }

    String& append(const String &rhs){
        char  *temp=new char[this->size()+rhs.size()+1]();
        strcpy(temp,_pstr);
        delete [] _pstr;
        strcat(temp,rhs._pstr);
        _pstr=temp;
        return *this;
    }

    String& append(const char *rhs){
        String temp(rhs);
        this->append(temp);
        return *this;
    }
    

private:
    char * _pstr;
};

ostream & operator<<(ostream &os,const String &rhs){
    if(rhs._pstr){
        os<<rhs._pstr;
    }
    return os;
}

istream & operator>>(istream &is,String &rhs){
    vector<char> vec;
    char ch;
    while((ch=is.get())!='\n'){
        vec.emplace_back(ch);
    }
    delete []rhs._pstr;
    rhs._pstr=new char[vec.size()+1]();
    for(size_t it=0;it<vec.size();it++){
        rhs._pstr[it]=vec[it];
    }
    return is;
}

bool operator==(const String &lhs,const String &rhs){
    return strcmp(lhs._pstr,rhs._pstr)==0;
}
bool operator!=(const String &lhs,const String &rhs){
    return strcmp(lhs._pstr,rhs._pstr)!=0;
}
bool operator>=(const String &lhs,const String &rhs){
    return strcmp(lhs._pstr,rhs._pstr)>=0;
}
bool operator<=(const String &lhs,const String &rhs){
    return strcmp(lhs._pstr,rhs._pstr)<=0;
}

String operator+(const String &lhs,const String &rhs){
    String temp(lhs);
    temp+=rhs;
    return temp;
}


void test(){
    String s1;
    cout << " --- " << s1 << " --- "<<endl;

    String s2("JAVA");
    cout << " --- " << s2 << " --- "<<endl;
    
    String s3="C++";
    cout << " --- " << s3 << " --- "<<endl;


    s2=s3;
    cout << " --- " << s2 << " --- "<<endl;

    String s4("hello");
    s4.append(s3);
    cout << " --- " << s4 << " --- "<<endl;

    s4.append(" test");
    cout << " --- " << s4 << " --- "<<endl;

    String s5(String("rightString"));
    cout << " --- " << s5 << " --- "<<endl;

    String s6("before");
    cout << " --- " << s6 << " --- "<<endl;
    
    cout << " --- " << "输入修改的s6:" << " --- "<<endl;

    cin>>s6;
    cout << " --- " << s6 << " --- "<<endl;

}

void test2(){
    String s1="KING";
    s1+=String(" Queen");
    cout<<" --- s1+=Queen\t"<<s1<<" --- "<<endl;

    String s3("last");
    String s4=s3+s1;
    cout<<" --- s3=last+s1\t"<<s3<<" --- "<<endl;

}

int main()
{
 test2();
 return 0;
}

